package poo.thread.buffer;

import poo.util.*;

public class BoundedBuffer<T> extends BufferLimitato<T> {// buffer limitato

	private Object lock = new Object();

	public BoundedBuffer(int n) {
		super(n);
	}

	public void put(T x) {
		synchronized (lock) {
			while (isFull()) {
				try {
					lock.wait();
				} catch (InterruptedException e) {

				}
				super.put(x);
				lock.notifyAll();// risveglia tutti cosi se qualche consumatore
									// aspetta che il buffer si riempia adesso
									// puo consumare.

			}

		}// synch
	}// put

	public T get() {
		synchronized (lock) {
			while (isEmpty())
				try {
					lock.wait();
				} catch (InterruptedException e) {

				}
			T x = super.get();
			lock.notifyAll();
			return x;

		}// synch
	}// get

	public boolean isFull() {
		synchronized (lock) {
			return super.isFull();
		}
	}

	public boolean isEmpty() {
		synchronized (lock) {
			return super.isEmpty();
		}
	}

	public String toString() {
		synchronized (lock) {
			return super.toString();
		}
	}

}
